package com.zhentao.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

/**
 * 测试数据初始化器
 * 确保数据库中有测试数据
 */
@Component
public class TestDataInitializer implements CommandLineRunner {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void run(String... args) throws Exception {
        try {
            // 检查是否有数据
            Integer count = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM data_collection", Integer.class);
            if (count == null || count == 0) {
                System.out.println("插入测试数据...");
                insertTestData();
                System.out.println("测试数据插入完成！");
            } else {
                System.out.println("数据库中已有 " + count + " 条数据");
            }
        } catch (Exception e) {
            System.out.println("检查数据失败: " + e.getMessage());
        }
    }

    private void insertTestData() {
        String sql = "INSERT INTO data_collection (data_type, customs_area, declare_no, item_no, currency_type, amount_cny, amount_usd, goods_quantity, status, import_export_type, customs_clearance_date, enterprise_name, enterprise_code, enterprise_credit_level, hs_code, goods_name, country_code, country_name, port_code, port_name, transport_mode) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        
        // 插入测试数据
        jdbcTemplate.update(sql, 1, "浦东", "223020230009876543", "0003", 1, 8900.75, 0.00, 10, 3, "IMPORT", "2023-10-01 14:20:00", "上海贸易有限公司", "SH001", "A", "8517120000", "手机", "KOR", "韩国", "1200", "上海浦东国际机场", "空运");
        jdbcTemplate.update(sql, 1, "洋山", "223020230001234567", "0001", 2, 0.00, 1500.50, 50, 2, "IMPORT", "2023-10-01 08:30:00", "北京科技有限公司", "BJ001", "A", "8471300000", "笔记本电脑", "USA", "美国", "1100", "北京首都国际机场", "空运");
        jdbcTemplate.update(sql, 2, "边民互市", "223020230007654321", "0002", 1, 35000.00, 0.00, 200, 2, "EXPORT", "2023-10-01 10:15:00", "深圳电子制造有限公司", "SZ001", "A", "8517120000", "手机配件", "USA", "美国", "2400", "深圳宝安国际机场", "空运");
        jdbcTemplate.update(sql, 4, "虹桥", "223020230004567890", "0004", 2, 0.00, 3200.00, 80, 1, "IMPORT", "2023-10-02 09:45:00", "广州进出口贸易公司", "GZ001", "B", "8471300000", "服务器", "DEU", "德国", "2300", "广州白云国际机场", "空运");
        jdbcTemplate.update(sql, 1, "洋山", "223020230002345678", "0005", 2, 0.00, 750.25, 30, 2, "EXPORT", "2023-10-02 11:30:00", "天津机械制造有限公司", "TJ001", "A", "8483300000", "轴承", "JPN", "日本", "0200", "天津滨海国际机场", "空运");
    }
} 
 
 
 
 
 

